package com.ddz.service;

import com.ddz.model.LoginMessage;
import com.ddz.model.enums.SystemEnums;
import com.ddz.util.JDBCUtil;
import com.ddz.util.StringUtil;
import org.json.JSONObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

public class UserRegisterService {
    private LoginMessage loginMessage;

    public UserRegisterService() {
        loginMessage = new LoginMessage();
    }

    public LoginMessage RegisterUser(String nickname, String userID, String password, String inviteCode) {
        JSONObject json = new JSONObject();
        json.put("nickname", nickname);
        json.put("userID", userID);
        json.put("password", password);
        // 邀请码
        String[] inviteCodeArray = {"lichen040609", "wadsp629", "2982788056" , "2830850220"};

        System.out.println("注册申请："+"nickname"+nickname+"userID"+userID+"password"+password+"inviteCode"+inviteCode);
        if (StringUtil.isEmpty(userID)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！账号为空！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        } else if (StringUtil.isEmpty(password)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！密码为空！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        } else if (StringUtil.isEmpty(nickname)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！昵称为空！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        } else if (StringUtil.isOver100(userID)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！账号超过100字符！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        } else if (StringUtil.isOver100(password)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！密码超过100字符！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        } else if (StringUtil.isOver100(nickname)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！昵称超过100字符！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        } else if (!StringUtil.isAlphaNumeric(userID)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！账号不符合规则！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        }else if (StringUtil.isEmpty(inviteCode)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！邀请码为空！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        }else if (StringUtil.isOver100(inviteCode)) {
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！邀请码错误！");
            loginMessage.setJsonObject(json);
            return loginMessage;
        }else if (!(Arrays.stream(inviteCodeArray).anyMatch(s -> s.equals(inviteCode)))){
            loginMessage.setStatus(100);
            loginMessage.setMsg("注册失败！邀请码错误！(注册码是贡献者的微信号或QQ号)");
            loginMessage.setJsonObject(json);
            return loginMessage;
        }
        try {
            JDBCUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        String sql = "SELECT * FROM " + SystemEnums.SQL_TABLE_USER.getValue() + " WHERE userId =?";
        ResultSet resultSet = JDBCUtil.executeQuery(sql, json.getString("userID"));
        try {
            // 判断账号是否存在
            if (resultSet.next()) {
                System.out.println("账号已存在！");
                loginMessage.setStatus(100);
                loginMessage.setMsg("账号已存在！");
                loginMessage.setJsonObject(json);
                return loginMessage;
            } else {
                String sql2 = "INSERT INTO " + SystemEnums.SQL_TABLE_USER.getValue() + "(nickname,userId, " +
                        "password,score)" +
                        " " +
                        "VALUES (?,?,?,?)";
                int rowsInserted = JDBCUtil.executeUpdate(sql2, json.getString("nickname"), json.getString("userID"),
                        json.getString("password"), "0");
                if (rowsInserted > 0) {
                    System.out.println("注册成功！");
                    loginMessage.setStatus(200);
                    loginMessage.setMsg("注册成功！");
                    loginMessage.setJsonObject(json);
                    return loginMessage;
                }
            }
        } catch (SQLException e) {
            System.out.println("SQL语句异常:{}" + e.getMessage());
            throw new RuntimeException(e);
        }
        return loginMessage;
    }
}
